package com.huawei.appmarket.service.settings.card;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.appgallery.foundation.ui.framework.activity.AbstractBaseActivity;
import com.huawei.appgallery.foundation.ui.framework.cardkit.bean.CardBean;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.framework.startevents.bean.CountryData;
import com.huawei.appmarket.framework.startevents.bean.CountryInfo;
import com.huawei.appmarket.service.appdetail.bean.report.GeneralResponse;
import com.huawei.appmarket.service.settings.bean.BaseSettingCardBean;
import com.huawei.cloudservice.CloudAccount;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.IntentResultHandler;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant.KeyAndValue;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.updatesdk.service.otaupdate.UpdateStatusCode.DialogButton;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import o.AccountBussinessListener;
import o.ccp;
import o.ccs;
import o.cfw;
import o.cgz;
import o.cvt;
import o.BaseRequest;
import o.dgh;
import o.dgm;
import o.dhq;
import o.diu;
import o.dky.j;
import o.Logger;
import o.dof;
import o.ContentManager;
import o.drh;
import o.duj;
import o.duo;
import o.dvt;
import o.dzu;
import o.eec;
import o.egn;
import o.egv;
import o.egw;
import o.ekg;
import o.eod;
import o.eoi;
import o.eoj;
import o.eop;
import o.eos;
import o.eot;
import o.CollectCheck;
import o.eqp;
import o.euf;
import o.ActivityUtil;
import o.evk;
import o.evx;

public class SettingChangeHomecountryCard extends BaseSettingCard {
    private String ˊ = UserSession.getInstance().getUserId();
    private Context ˋ;
    private TextView ˎ;
    private evx ˏ = new evx(this) {
        final /* synthetic */ SettingChangeHomecountryCard ˋ;

        {
            this.ˋ = r1;
        }

        public void ˋ(View view) {
            dgh.onEvent(new o.dge.d(this.ˋ.ˋ, o.der.f.ʼˊ).ˏ(KeyAndValue.NUMBER_01 + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + this.ˋ.ˊ + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + this.ˋ.ॱ).ˋ());
            if (UserSession.getInstance().isLoginSuccessful()) {
                this.ˋ.ˋ(1);
                return;
            }
            eoj.ˋ().ˎ("home_country_observer_key", new e(this.ˋ));
            eod.ˊ(this.ˋ.ˋ, null, false, true, true);
        }
    };
    private String ॱ = eqp.ॱ();

    public interface a {
        void ͺ();

        void ᐝॱ();
    }

    static class b extends ccs {
        private String ˊ;

        public b(String str) {
            this.ˊ = str;
        }

        public void onFinish(Bundle bundle) {
            Logger.ˎ("SettingChangeHomecountryCard", "[global]  MyCloudRequestHandler onFinish");
        }

        public void onError(ErrorStatus errorStatus) {
            super.onError(errorStatus);
            Logger.e("SettingChangeHomecountryCard", "[global] " + " updateHwIDonError, ErrorCode: " + errorStatus.getErrorCode() + ", ErrorReason: " + errorStatus.getErrorReason());
        }

        public String ˋ() {
            return this.ˊ;
        }
    }

    class c implements o.ckh.a {
        final /* synthetic */ SettingChangeHomecountryCard ˋ;

        private c(SettingChangeHomecountryCard settingChangeHomecountryCard) {
            this.ˋ = settingChangeHomecountryCard;
        }

        public void ˏ(AlertDialog alertDialog, com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity.DialogActivity.a aVar, int i) {
            if (-1 == i) {
                this.ˋ.ˊ();
            }
        }
    }

    public static class d extends eot {
        private WeakReference<Activity> ˊ;
        private int ˋ;
        private a ˎ;
        private int ˏ;
        private Runnable ॱ;

        public d(Activity activity, int i, a aVar, int i2, Runnable runnable) {
            this.ˊ = new WeakReference(activity);
            this.ˋ = i;
            this.ˎ = aVar;
            this.ˏ = i2;
            this.ॱ = runnable;
        }

        public void onFinish(Intent intent) {
            if (intent != null) {
                Activity activity = (Activity) this.ˊ.get();
                if (activity != null && !activity.isFinishing()) {
                    List<CountryData> ˏ = dzu.ˏ(this.ˋ);
                    if (CollectCheck.isEmpty(ˏ)) {
                        Logger.ˋ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard country list cache is empty! ");
                        cvt o_drh = new drh("supportCountry");
                        o_drh.setServiceType_(this.ˋ);
                        cfw.ˎ(o_drh, new f(intent, activity, this.ˋ));
                    } else {
                        Logger.ˎ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard country list cache not empty! ");
                        ArrayList arrayList = new ArrayList();
                        for (CountryData ॱ : ˏ) {
                            arrayList.add(ॱ.ॱ());
                        }
                        SettingChangeHomecountryCard.ॱ(activity, arrayList, intent);
                    }
                } else {
                    return;
                }
            }
            Logger.ˋ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard IntentResultHandler intent is null");
            if (this.ˎ != null) {
                this.ˎ.ͺ();
            }
        }

        public void onError(ErrorStatus errorStatus) {
            if (this.ˎ != null) {
                this.ˎ.ᐝॱ();
            }
            Logger.ˋ("GLOBAL_START_FLOW", "SettingChangeHomecountryCardMyIntentResultHandler onError  " + errorStatus.toString());
            super.onError(errorStatus);
            int errorCode = errorStatus.getErrorCode();
            Activity activity = (Activity) this.ˊ.get();
            boolean z = (this.ˏ <= 0 || this.ॱ == null || ActivityUtil.isAcitivtyNotValid(activity)) ? false : true;
            if (35 == errorCode) {
                CloudAccountManager.updateHwId(ContentManager.getInstant().getContext(), new Bundle(), new b("updateHwID"));
            } else if (34 == errorCode) {
                CloudAccountManager.initial(ContentManager.getInstant().getContext(), new Bundle(), new b("initial"));
            } else if (40 == errorCode && z) {
                Logger.ˋ("GLOBAL_START_FLOW", "SettingChangeHomecountryCardMyIntentResultHandler AIDL failed, remaining retry times = " + this.ˏ);
                eod.ˏ(activity, new eoi(this) {
                    final /* synthetic */ d ˏ;

                    {
                        this.ˏ = r1;
                    }

                    public void ˎ(int i) {
                        this.ˏ.ॱ.run();
                    }
                }, true);
            } else {
                eoj.ˋ().ˋ(new ccp(DialogButton.CONFIRM, 10101));
                evk.ˏ(ContentManager.getInstant().getContext(), j.ॱॱ, 0).ˋ();
            }
        }

        public String ˊ() {
            return "getHomeCountryChangeIntent";
        }
    }

    static class e implements AccountBussinessListener {
        WeakReference<SettingChangeHomecountryCard> ॱ;

        e(SettingChangeHomecountryCard settingChangeHomecountryCard) {
            this.ॱ = new WeakReference(settingChangeHomecountryCard);
        }

        public void onAccountBusinessResult(ccp o_ccp) {
            if (o_ccp.ˎ == 102) {
                SettingChangeHomecountryCard settingChangeHomecountryCard = (SettingChangeHomecountryCard) this.ॱ.get();
                if (settingChangeHomecountryCard != null) {
                    settingChangeHomecountryCard.ˋ(1);
                }
            }
        }
    }

    static class f implements IServerCallBack {
        private int ˋ;
        private WeakReference<Activity> ˎ;
        private Intent ॱ;

        public f(Intent intent, Activity activity, int i) {
            this.ॱ = intent;
            this.ˎ = new WeakReference(activity);
            this.ˋ = i;
        }

        public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        }

        public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
            Activity activity = (Activity) this.ˎ.get();
            if (activity != null && !activity.isFinishing() && (responseBean instanceof GeneralResponse) && responseBean.getResponseCode() == 0) {
                GeneralResponse generalResponse = (GeneralResponse) responseBean;
                if (generalResponse.getRtnCode_() == 0) {
                    CountryInfo ʻ = generalResponse.ʻ();
                    if (ʻ != null) {
                        List<CountryData> ˋ = ʻ.ˋ();
                        ArrayList arrayList = new ArrayList();
                        if (CollectCheck.isEmpty(ˋ)) {
                            Logger.ˋ("SettingChangeHomecountryCard", "Global country list is null");
                            return;
                        }
                        dzu.ॱ(ˋ, this.ˋ);
                        for (CountryData ॱ : ˋ) {
                            arrayList.add(ॱ.ॱ());
                        }
                        SettingChangeHomecountryCard.ॱ(activity, arrayList, this.ॱ);
                        return;
                    }
                    Logger.ˋ("SettingChangeHomecountryCard", "Global countryInfo is null");
                }
            }
        }
    }

    public SettingChangeHomecountryCard(Context context) {
        super(context);
        this.ˋ = context;
    }

    private static void ॱ(Activity activity, ArrayList<String> arrayList, Intent intent) {
        String ॱ = eqp.ॱ();
        try {
            intent.putStringArrayListExtra("serviceCountryCodeList", arrayList);
            intent.putExtra("serviceCountryCode", ॱ);
        } catch (Exception e) {
            Logger.ˋ("SettingChangeHomecountryCard", "goHomeCountryChangePage intent putExtra exception:" + e.toString());
        }
        if (activity == null || activity.isFinishing()) {
            Logger.ˋ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard IntentResultHandler mActivity is finish ");
            return;
        }
        Logger.ˎ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard startActivityForResult ");
        try {
            activity.startActivityForResult(intent, 10008);
        } catch (ActivityNotFoundException e2) {
            Logger.ˋ("SettingChangeHomecountryCard", "ActivityNotFoundException :" + e2.toString());
        }
    }

    public cgz ˏ(View view) {
        super.ˏ(view);
        this.ˎ = (TextView) view.findViewById(o.der.d.ʽʽ);
        view.findViewById(o.der.d.ʽʻ).setVisibility(8);
        view.findViewById(o.der.d.ʿˊ).setMinimumHeight(this.ʼॱ.getResources().getDimensionPixelSize(o.der.b.ˊ));
        ((TextView) view.findViewById(o.der.d.ʽʼ)).setText(o.der.f.ˊʻ);
        ʽ(view);
        return this;
    }

    public void ˎ() {
        this.ˎ.setText(eqp.ˊ());
    }

    public void ˏ(CardBean cardBean) {
        super.ˏ(cardBean);
        ˎ();
        this.ˊˋ.setOnClickListener(this.ˏ);
        if (cardBean != null && (cardBean instanceof BaseSettingCardBean)) {
            BaseSettingCardBean baseSettingCardBean = (BaseSettingCardBean) cardBean;
            int ꜞ = baseSettingCardBean.ꜞ();
            if (ꜞ == 10008) {
                ˋ(ꜞ, baseSettingCardBean.ㆍ(), baseSettingCardBean.ꓸ());
            }
        }
    }

    private void ˊ(Context context) {
        Logger.ˎ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard showChangDlg");
        String ˊ = eqp.ˊ();
        new com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity.DialogActivity.a(context, "HomeCountryChangeDialog").ॱ(context.getString(o.der.f.ᐧॱ, new Object[]{ˊ})).ˊ(-2, 8).ˏ(new c()).ˋ(-1, o.der.f.ॱʿ).ॱ(false).ˎ();
    }

    private void ˊ() {
        Logger.ˎ("SettingChangeHomecountryCard", "StartupCallback showProtocol");
        diu.ˊ().ॱ((Activity) this.ˋ, true, new o.dio.c(this) {
            final /* synthetic */ SettingChangeHomecountryCard ˏ;

            {
                this.ˏ = r1;
            }

            public void ॱ(boolean z) {
                if (z) {
                    egw.ˏ().ˋ();
                } else {
                    AbstractBaseActivity.sendUpgrade(this.ˏ.ˋ);
                }
            }
        });
    }

    public void ˋ(int i, int i2, Intent intent) {
        Logger.ˎ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard" + " onActivityResult resultCode=" + i2 + " requestCode=" + i);
        if (i == 10008 && intent != null) {
            if (-1 == i2) {
                String stringExtra = intent.getStringExtra(HwAccountConstants.EXTRA_SERVICE_COUNTRY);
                String ॱ = eqp.ॱ();
                if (dof.ˋ(stringExtra) || stringExtra.equalsIgnoreCase(ॱ)) {
                    Logger.ˋ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard returnHomeCountry is blank");
                    return;
                }
                Logger.ˋ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard restartApplication ");
                dgh.ˏ();
                diu.ˊ().ॱ();
                duj.ˏ().ˎ(duo.class, new Object[0]);
                dhq.ॱ().ˊ();
                euf.ˎ().ॱॱ();
                dvt.ˏ().ˋ(1);
                ekg.ॱ().ˏ();
                UserSession.getInstance().setHomeCountry(stringExtra);
                eop.ˎ(UserSession.getInstance());
                egv.ॱᐝ().ˏॱ();
                egn.ˎ().ॱ();
                eec.ˊ().ˏ();
                ˊ(this.ˋ);
                return;
            }
            ErrorStatus errorStatus = (ErrorStatus) intent.getParcelableExtra(HwAccountConstants.EXTRA_PARCE);
            if (errorStatus != null) {
                Logger.ˋ("GLOBAL_START_FLOW", "onError errorCode:" + errorStatus.getErrorCode() + ", errorReason:" + errorStatus.getErrorReason());
            } else {
                Logger.ˋ("GLOBAL_START_FLOW", "SettingChangeHomecountryCard errStatus is null");
            }
        }
    }

    private void ˋ(final int i) {
        Context ˋ = ContentManager.getInstant().getContext();
        IntentResultHandler dVar = new d((Activity) this.ˋ, dgm.ˎ((Activity) this.ˋ), null, i, new Runnable(this) {
            final /* synthetic */ SettingChangeHomecountryCard ॱ;

            public void run() {
                this.ॱ.ˋ(i - 1);
            }
        });
        CloudAccount ˊ = eos.ˊ();
        if (ˊ != null) {
            ˊ.getHomeCountryChangeIntent(ˋ, dVar);
        } else {
            Logger.e("SettingChangeHomecountryCard", "CloudAccount is null");
        }
    }
}
